package com.guo.xiao.bing.controller;

import com.guo.xiao.bing.dao.ProjectMapper;
import com.guo.xiao.bing.entity.JoinProject;
import com.guo.xiao.bing.entity.Project;
import com.guo.xiao.bing.entity.ProjectType;
import com.guo.xiao.bing.entity.User;
import com.guo.xiao.bing.service.JoinProjectService;
import com.guo.xiao.bing.service.ProjectService;
import com.guo.xiao.bing.service.ProjectTypeService;
import com.guo.xiao.bing.vo.JoinProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("join")
public class JoinProjectController {

    @Autowired
    ProjectTypeService projectTypeService;
    @Autowired
    ProjectService projectService;
    @Autowired
    JoinProjectService joinProjectService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String list(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        List<JoinProjectVo> joinProjects = null;

        if ("admin".equals(user.getUsername())) {
            joinProjects = joinProjectService.find();
        } else {
            joinProjects = joinProjectService.findByUser(user.getId());
        }

        model.addAttribute("joinProjects", joinProjects);
        return "join/list";
    }

    @RequestMapping(value = "finish", method = RequestMethod.GET)
    public String finish(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        List<JoinProjectVo> joinProjects = null;

        if ("admin".equals(user.getUsername())) {
            joinProjects = joinProjectService.find(3);
        } else {
            joinProjects = joinProjectService.findByUser(3, user.getId());
        }

        model.addAttribute("joinProjects", joinProjects);
        return "join/list";
    }

    @RequestMapping(value = "add/{projectId}", method = RequestMethod.GET)
    public String add(@PathVariable Integer projectId, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        Project project = projectService.findById(projectId);
        model.addAttribute(project);
        if (project == null) {
            return "redirect:/user/1";
        }
        ProjectType projectType = projectTypeService.findById(project.getProjectTypeId());
        model.addAttribute(projectType);
        return "join/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@ModelAttribute JoinProject joinProject, Integer projectId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        joinProject.setUserId(user.getId());
        joinProject.setOrgId(projectId);
        joinProjectService.save(joinProject);
        return "redirect:/join";
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        joinProjectService.delete(id);
        return "redirect:/join";
    }

    @RequestMapping(value = "del/{id}/{status}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, @PathVariable Integer status, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        JoinProject jp = joinProjectService.findById(id);
        jp.setStatus(status);
        joinProjectService.update(jp);
        return "redirect:/join";
    }

}
